using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.CompositeUI;
using System.Windows.Forms;
using Microsoft.Practices.CompositeUI.Commands;

namespace SCSFContrib.CompositeUI.WinForms
{
    /// <summary>
    /// A helper class for dealing with commands.
    /// </summary>
    public static class CommandHelper
    {
        /// <summary>
        /// Adds new items as invoker for the specified command.
        /// </summary>
        public static void AddInvoker(WorkItem workItem, string commandName, params object[] items)
        {
            AddInvoker(workItem, commandName, "Click", items);
        }

        /// <summary>
        /// Adds new items as invoker for the specified command.
        /// </summary>
        public static void AddInvoker(WorkItem workItem, string commandName, string eventName, params object[] items)
        {
            bool enabled = (workItem.Commands[commandName].Status == CommandStatus.Enabled) ? true : false;
            foreach (object item in items)
            {
                workItem.Commands[commandName].AddInvoker(item, eventName);

                if (item is Control) { ((Control)item).Enabled = enabled; }
                if (item is ToolStripItem) { ((ToolStripItem)item).Enabled = enabled; }
            }
        }

        /// <summary>
        /// Set the status of the command.
        /// </summary>
        public static void SetStatus(WorkItem workItem, string commandName, bool enabled)
        {
            CommandStatus status = enabled ? CommandStatus.Enabled : CommandStatus.Disabled;
            SetStatus(workItem, commandName, status);
        }

        /// <summary>
        /// Set the status of the command.
        /// </summary>
        public static void SetStatus(WorkItem workItem, string commandName, CommandStatus status)
        {
            workItem.Commands[commandName].Status = status;
        }
    }
}
